<template>
  <div class="app-container">
    <el-table
      :data="
        tableData.slice((currentPage - 1) * PageSize, currentPage * PageSize)
      "
      style="width: 100%"
    >
      <el-table-column label="名称">
        <template slot-scope="scope">
          <div @click="showSlide(scope.row)" style="cursor: pointer">
            {{ scope.row.name }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="_id" label="id" />
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img
            :src="scope.row.coverImg | handleImgPath"
            width="100"
            height="100"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="editCategory(scope.row)"
            >修改</el-button
          >
          <el-button type="danger" size="small" @click="delCategory(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改类别信息" :visible.sync="dialogVisible" width="60%">
      <el-form ref="category" :model="category" label-width="80px">
        <el-form-item label="名字">
          <el-input v-model="category.name" />
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="http://lanlianhua.work:8888/api/v1/common/file_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editsure">确 定</el-button>
      </span>
    </el-dialog>
    <el-pagination
      background
      layout="prev, pager, next,jumper"
      :total="totalCount"
      :page-size="PageSize"
      @current-change="handleCountChange"
    />
    <SlideView
      :show="showFlag"
      :showFlag.sync="showFlag"
      :message="message"
    ></SlideView>
  </div>
</template>

<script>
import { getCategory, editCategoty, delCategory } from "@/api/category";
import SlideView from "@/views/mixin/slideminxin";
export default {
  mixins: [SlideView],
  name: "CategoryList",
  components: {},
  data() {
    return {
      PageSize: 10,
      totalCount: 0,
      currentPage: 1,
      tableData: [],
      dialogVisible: false,
      imageUrl: "", // 表示图片路径
      id: "",
      category: {
        name: "",
        id: "",
      },
    };
  },

  created() {
    this.initCategory();
  },

  methods: {
    handleCountChange(val) {
      this.currentPage = val;
    },
    handleImgPath(str) {
      if (str) {
        if (str.startsWith("http") || str.startsWith("data")) {
          return str;
        } else {
          return "http://lanlianhua.work:8888" + str;
        }
      } else {
        return "http://www.superslide2.com/demo/images/pic3.jpg";
      }
    },
    async initCategory() {
      let data = {
        per: 200,
        page: this.currentPage,
        // name:"口红"
      };
      const result = await getCategory(data);
      this.tableData = result.categories;
      this.totalCount = result.categories.length;
    },
    //上传头像
    handleAvatarSuccess(res, file) {
      this.imageUrl = "http://lanlianhua.work:8888" + res.info;
    },
    // 弹出修改弹框
    editCategory(category) {
      this.dialogVisible = true;
      this.category.name = category.name;
      this.imageUrl = this.handleImgPath(category.coverImg);
      this.id = category._id;
    },
    // 确定修改
    async editsure() {
      const reuslt = await editCategoty(this.id, {
        name: this.category.name,
        coverImg: this.imageUrl,
      });

      // 关闭弹框
      this.dialogVisible = false; // 关闭弹框
      this.initCategory(); // 再次调用接口
    },
    //删除
    async delCategory(category) {
      const result = await delCategory(category._id);
      this.initCategory();
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
@import "@/styles/edit.scss";
.el-pagination {
  width: 100%;
  margin: 50px auto;
}
.el-pagination >>> li,
.el-pagination >>> button {
  width: 40px;
  height: 40px;
  text-align: center;
  line-height: 40px;
}
</style>
